/**
 * 文件名：TransferFacadeImpl.java
 *
 * 创建人：Elvin Zhao - 279152260@qq.com
 *
 * 创建时间：2019年1月3日 下午3:59:21
 *
 * 版权所有：FNII
 */
package com.z100h.supplychain.evidence.facade.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSON;
import com.jeeplus.core.facade.FacadeException;
import com.jeeplus.modules.sys.entity.User;
import com.jeeplus.modules.sys.utils.UserUtils;
import com.z100h.supplychain.core.EvidenceState;
import com.z100h.supplychain.core.EvidenceType;
import com.z100h.supplychain.core.biz.entity.CommonEvidence;
import com.z100h.supplychain.core.process.AbstractTransferProcess;
import com.z100h.supplychain.evidence.entity.TransferEvidence;
import com.z100h.supplychain.evidence.facade.TransferFacade;
import com.z100h.supplychain.evidence.service.TransferEvidenceService;
import com.z100h.supplychain.sku.entity.SkuAmount;
import com.z100h.supplychain.statistics.service.StatRealtimeResourceService;
import com.z100h.supplychain.warehouse.service.WarehouseService;

/**
 * [描述信息：说明类的基本功能]
 *
 * @author Elvin Zhao - 279152260@qq.com
 * @version 1.0 Created on 2019年1月3日 下午3:59:21
 */
@Service
public class TransferFacadeImpl extends AbstractTransferProcess<TransferEvidence>
		implements TransferFacade<TransferEvidence> {

	@Autowired
	private TransferEvidenceService transferEvidenceService;

	@Autowired
	private StatRealtimeResourceService statRealtimeResourceService;

	@Autowired
	private WarehouseService warehouseService;

	/**
	 * {@inheritDoc}
	 * 
	 * @see com.z100h.supplychain.core.process.AbstractTransferProcess#createEvidence(com.z100h.supplychain.core.Evidence, com.z100h.supplychain.core.EvidenceState)
	 * @author: Elvin Zhao - 279152260@qq.com 
	 * @date: Created on 2019年1月3日 下午3:59:36
	 */
	@Override
	protected String createEvidence(TransferEvidence evidence, EvidenceState evidenceState, EvidenceType evidenceType) {
		CommonEvidence commonEvidence = new CommonEvidence(evidenceType, evidenceState);

		// 插入主表凭证
		commonEvidenceService.save(commonEvidence);

		// 插入转库凭证
		evidence.setEvidenceId(commonEvidence.getId());
		transferEvidenceService.save(evidence);
		return commonEvidence.getId();
	}

	/**
	 * {@inheritDoc}
	 * 
	 * @see com.z100h.supplychain.core.process.AbstractTransferProcess#changeResourceWithEvidence(com.z100h.supplychain.core.Evidence, java.util.List)
	 * @author: Elvin Zhao - 279152260@qq.com 
	 * @date: Created on 2019年1月3日 下午3:59:36
	 */
	@Override
	protected void changeResourceWithEvidence(TransferEvidence evidence, List<String> resourceIds) {
		resourceService.updateResourceToWarehouseId(resourceIds, evidence.getWarehouseId());
	}

	/**
	 * {@inheritDoc}
	 * 
	 * @see com.z100h.supplychain.evidence.facade.TransferFacade#transferProcess(java.util.List, java.lang.Object)
	 * @author: Elvin Zhao - 279152260@qq.com 
	 * @return 
	 * @date: Created on 2019年1月3日 下午4:11:50
	 */
	@Override
	@Transactional(readOnly = false)
	public String transferProcess(List<String> resourceIds, TransferEvidence evidence, EvidenceType evidenceType) {
		try {
			return process(resourceIds, evidence, evidenceType);
		} catch (FacadeException e) {
			logger.error(e.getMessage());
			throw new FacadeException(e);
		}
	}

	/**
	 * {@inheritDoc}
	 * 
	 * @see com.z100h.supplychain.evidence.facade.TransferFacade#save(java.lang.Object)
	 * @author: Elvin Zhao - 279152260@qq.com 
	 * @date: Created on 2020年7月7日 上午10:14:51
	 */
	@Override
	public void save(TransferEvidence transferEvidence) {
		transferEvidenceService.save(transferEvidence);
	}

	/**
	 * {@inheritDoc}
	 * 
	 * @see com.z100h.supplychain.evidence.facade.TransferFacade#save(java.lang.Object, java.util.List)
	 * @author: Elvin Zhao - 279152260@qq.com 
	 * @return 
	 * @date: Created on 2020年7月7日 上午10:18:23
	 */
	@Override
	@Transactional(readOnly = false)
	public String save(EvidenceType evidenceType, String warehouseType, List<SkuAmount> list, String comment) {
		User user = UserUtils.getUser();

		/**
		 * 1.获取转库资源数量，并检查
		 * 2.创建转库单
		 * 3.转库逻辑
		 */
		if (CollectionUtils.isNotEmpty(list)) {
			/**
			 * 1.获取转库资源数量
			 * 并检查
			 */
			List<String> resourceIds = new ArrayList<String>();
			for (SkuAmount skuAmount : list) {
				Integer amount = 0;
				List<String> skuResourceIds = statRealtimeResourceService.findResourceIds(skuAmount,
						user.getCompanyId());
				if (CollectionUtils.isNotEmpty(skuResourceIds)) {
					amount = skuResourceIds.size();
				}
				if (!skuAmount.getAmount().equals(amount)) {
					logger.error("库存不足，无法转库，异常SKUID：" + skuAmount.getSkuId());
					throw new FacadeException("库存不足，无法转库，异常SKUID：" + skuAmount.getSkuId());
				}
				resourceIds.addAll(skuResourceIds);
			}

			/**
			 * 2.保存转库单
			 */
			String warehouseId = warehouseService.getWarehouseByType(warehouseType, user.getCompanyId());
			if (warehouseId == null) {
				logger.error("仓库不存在，无法转库，异常仓库类型：" + warehouseType);
				throw new FacadeException("仓库不存在，无法转库，异常仓库类型：" + warehouseType);
			}

			TransferEvidence transferEvidence = new TransferEvidence();
			transferEvidence.preInsert();
			transferEvidence.setWarehouseType(warehouseType);
			transferEvidence.setWarehouseId(warehouseId);
			transferEvidence.setComment(comment);
			transferEvidence.setTransferReason("1");
			/**
			 * 3.转库逻辑
			 */
			return this.transferProcess(resourceIds, transferEvidence, evidenceType);
		} else {
			logger.error("转库单SKU为空，无法转库！");
			throw new FacadeException("转库单SKU为空，无法转库！");
		}

	}

	/**
	 * {@inheritDoc}
	 * 
	 * @see com.z100h.supplychain.evidence.facade.TransferFacade#saveCheckTransfer(java.util.List, java.lang.String)
	 * @author: Elvin Zhao - 279152260@qq.com 
	 * @date: Created on 2021年8月23日 下午12:12:35
	 */
	@Override
	@Transactional(readOnly = false)
	public String saveCheckTransfer(String companyId, String skuAmountJsonStr, String remarks) throws Throwable {

		try {
			UserUtils.loginByCompanyId(companyId);
			List<SkuAmount> skuAmountList = JSON.parseArray(skuAmountJsonStr, SkuAmount.class);
			String warehouseType = "cf6b44a54efc406ca1f0fe15bb325556";
			return save(EvidenceType.CHECKTRANSFER, warehouseType, skuAmountList, remarks);
		} catch (Throwable e) {
			throw new Exception(e.getMessage());
		}

	}

}
